﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Nhom14_HTTTHD_BuyMe.function
{
    public class Transaction
    {
        static public int AddTransaction(int bidID, int type, float price)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    transaction t = new transaction();

                    t.id_bid = bidID;
                    t.type_bid = type;
                    t.total = price;
                    t.timestamp = DateTime.Now;
                    t.status = "0";
                    context.transactions.AddObject(t);
                    context.SaveChanges();

                    return t.id;
                }
            }
            catch
            {
                return 0;
            }
        }

        static public int ChoosePaymentType(int iTranID, int type_w)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    transaction t = (from x in context.transactions
                                     where x.id == iTranID
                                     select x).FirstOrDefault();
                    if (t != null)
                    {
                        t.payment_type = 1;
                        t.status = "1";
                        if (type_w == 2)
                        {
                            t.payment_type = 2;
                            t.status = "3";
                        }
                        context.SaveChanges();
                    }
                    else
                    {
                        return 0;
                    }
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }

       /* static public int TransactionAT(int iTranID)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    transaction t = (from x in context.transactions
                                     where x.id == iTranID
                                     select x).FirstOrDefault();
                    if (t != null)
                    {
                        t.payment_type = 1;
                        t.status = "1";
                        context.SaveChanges();
                    }
                    else
                    {
                        return 0;
                    }
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }
            */
        static public int UpdateTransactionInformation(int id, string Information)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var result = from x in context.transactions
                                 where x.id == id
                                 select x;

                    if (result.Count() == 0)
                        return 0;

                    transaction t = result.First();

                    t.information = Information;
                    t.status = "2";
                    context.SaveChanges();
                    return t.id;
                }
            }
            catch
            {
                return 0;
            }
        }


        static public int GetBuyer(int id)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var result = from x in context.transactions
                                 where x.id == id
                                 select x;

                    if (result.Count() == 0)
                        return 0;

                    transaction t = result.First();

                    if (t.type_bid == 1)
                    {
                        return BidWebsite.GetAccountBid((int)t.id_bid);
                    }
                    else return BidDomain.GetAccountBid((int)t.id_bid);
                }
            }
            catch
            {
                return 0;
            }
        }

        static public int ChangeStatus(int id, string ss)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var result = from x in context.transactions
                                 where x.id == id
                                 select x;

                    if (result.Count() == 0)
                        return 0;

                    transaction t = result.First();

                    t.status = ss;
                    context.SaveChanges();
                    return 1;
                }
            }
            catch
            {
                return 0;
            }
        }

        static public int GetStatus(int id)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var result = from x in context.transactions
                                 where x.id == id
                                 select x;

                    if (result.Count() == 0)
                        return 0;

                    transaction t = result.First();

                    return Convert.ToInt32(t.status);
                }
            }
            catch
            {
                return 0;
            }
        }

        static public int GetSeller(int id)
        {
            try
            {
                using (websitebrokenEntities context = new websitebrokenEntities())
                {
                    var result = from x in context.transactions
                                 where x.id == id
                                 select x;

                    if (result.Count() == 0)
                        return 0;

                    transaction t = result.First();

                    if (t.type_bid == 1)
                    {
                        int w = BidWebsite.GetWebsiteID((int)t.id_bid);
                        return Website.GetAccountID(w);
                    }
                    else
                    {
                        int d = BidDomain.GetDomainID((int)t.id_bid);
                        return Domain.GetAccountID(d);
                    }
                }
            }
            catch
            {
                return 0;
            }
        }
    }
}